const mongoose = require('mongoose');
const config = require('../src/config/config');
const Admin = require('../src/models/admin.model');

const updateAdminPermissions = async () => {
  try {
    // 连接数据库
    await mongoose.connect(config.mongoose.url, config.mongoose.options);
    console.log('已连接到数据库');

    // 更新所有管理员的权限
    const result = await Admin.updateMany(
      {},
      {
        $set: {
          permissions: ['manageUsers', 'manageContent', 'manageSystem', 'all']
        }
      }
    );

    console.log('更新结果:', result);
    console.log(`成功更新了 ${result.modifiedCount} 个管理员的权限`);

    // 查看更新后的管理员列表
    const admins = await Admin.find({}, 'username role permissions');
    console.log('\n更新后的管理员列表:');
    admins.forEach(admin => {
      console.log(`- ${admin.username} (${admin.role}):`, admin.permissions);
    });

  } catch (error) {
    console.error('更新管理员权限时出错:', error);
  } finally {
    await mongoose.disconnect();
    console.log('已断开数据库连接');
  }
};

updateAdminPermissions(); 